package com.jzg.common.cron4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.cron4j.ITask;
import com.jzg.common.model.sys.SysUser;
import com.jzg.common.util.YTUtil;

public class PHOTOTask implements ITask{
	
	protected final Log log = Log.getLog(getClass());
	private boolean isEnd = true;
	private static String warnPath = PropKit.use("app-config-dev.txt").get("warnPath");
	ExecutorService executor = Executors.newFixedThreadPool(1);

	@Override
	public void run() {
		if (!isEnd)return;
		isEnd = false;
	    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
	        @Override
	        public Integer get() {
	        	try {
	        		List<Record> records=Db.use("plga").find("select * from Manage where NState <> '-1' ");
		        	log.info("==================开始同步用户头像====================");
		        	SysUser user;
		        	for(Record record:records) {
		        		user = SysUser.dao.findById(record.getStr("NId"));
		        		if(user!=null) {
		        			user.setCREATETIME(new Date(System.currentTimeMillis()));
		        			if(!StrKit.isBlank(record.getStr("SPath"))) {
		        				String base64Str = YTUtil.imgBase64(warnPath+record.getStr("SPath"));
		        				user.setPHOTO(base64Str);
		        				user.setPHOTOIMG(YTUtil.getImgBaseStr(warnPath+record.getStr("SPath"),base64Str));
		        			}
		        			user.setSRC("1");
		        			user.update();
		        		}
		        	}
				} catch (Exception e) {
					log.info(e.getMessage());
				}
	        	isEnd = true;
	            return 1;
	        }
	    }, executor);
	    future.thenAccept(e -> {
	    	isEnd = true;
	    });
	}

	@Override
	public void stop() {
		
	}

}
